﻿using Learun.Application.Organization;
using Learun.Util;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using static Learun.Application.TwoDevelopment.ZZDT_EC.ProjectSugar;
using static Learun.Util.SqlSugar.SqlSugarHelper;

namespace Learun.Application.TwoDevelopment.ZZDT_EC
{
    /// <summary>
    /// 版 本 PIT-ADMS V7.0.3 敏捷开发框架
    /// Copyright (c) 2013-2018 Hexagon PPM
    /// 创 建：超级管理员
    /// 日 期：2022-02-09 22:05
    /// 描 述：样板目录
    /// </summary>
    public class ec_template_catalogueBLL : ec_template_catalogueIBLL
    {
        private ec_template_catalogueService ec_template_catalogueService = new ec_template_catalogueService();

        #region 获取数据
        /// <summary>
        /// Tree类型，用于插件端新建图纸时，进行选择
        /// </summary>
        /// <param name="projectId"></param>
        /// <returns></returns>
        public List<TreeModel> GetTemplateCategory(string projectId)
        {
            ec_template_fileBLL ec_template_fileBLL = new ec_template_fileBLL();
            List<TreeModel> treeList = new List<TreeModel>();
            string queryJson = "{\"ProjectId\":\"" + projectId + "\"}";
            var listCatalogue = this.GetList(queryJson);
            var listFile = ec_template_fileBLL.GetList(queryJson);
            foreach (var itemCatalogue in listCatalogue)
            {
                TreeModel nodeCatalogue = new TreeModel();
                nodeCatalogue.id = itemCatalogue.TemplateCatalogueID;
                nodeCatalogue.text = itemCatalogue.TemplateCatalogueName;
                nodeCatalogue.value = itemCatalogue.TemplateCatalogueID;
                nodeCatalogue.nodeType = "0";
                nodeCatalogue.showcheck = false;
                nodeCatalogue.checkstate = 0;
                nodeCatalogue.isexpand = false;
                nodeCatalogue.parentId = itemCatalogue.UpTemplateCatalogueID;
                nodeCatalogue.NodeExtData = itemCatalogue;
                treeList.Add(nodeCatalogue);
                var listFileTmp = listFile.Where(x => x.TemplateCatalogueID == itemCatalogue.TemplateCatalogueID).ToList();

                foreach (var itemFile in listFileTmp)
                {
                    TreeModel nodeFile = new TreeModel();
                    nodeFile.id = itemFile.TemplateFileID;
                    nodeFile.text = itemFile.TemplateFileName;
                    nodeFile.value = itemFile.TemplateFileID;
                    nodeFile.nodeType = "1";
                    nodeFile.showcheck = false;
                    nodeFile.checkstate = 0;
                    nodeFile.isexpand = false;
                    nodeFile.parentId = itemCatalogue.TemplateCatalogueID;
                    nodeFile.NodeExtData = itemFile;
                    treeList.Add(nodeFile);
                }
            }

            return treeList.ToTree();
        }
        /// <summary>
        /// 获取列表数据
        /// <summary>
        /// <returns></returns>
        public IEnumerable<ec_template_catalogueEntity> GetList(string queryJson, Pagination pagination = null)
        {
            try
            {
                var queryParam = queryJson.ToJObject();
                var ProjectId = queryParam["ProjectId"]?.ToString();
                var TemplateCatalogueName = queryParam["TemplateCatalogueName"]?.ToString();
                var CurTemplateCatalogueID = queryParam["CurTemplateCatalogueID"]?.ToString();

                var tableName = TableName<ec_template_catalogueEntity>(ProjectId);
                var query = Db.Queryable<ec_template_catalogueEntity>().AS(tableName)
                    .WhereIF(!string.IsNullOrEmpty(TemplateCatalogueName), x => x.TemplateCatalogueName.Contains(TemplateCatalogueName))
                    .WhereIF(!string.IsNullOrEmpty(CurTemplateCatalogueID), x => !x.FullPath.Contains(CurTemplateCatalogueID))
                    .OrderBy(x => x.OrderID);
                List<ec_template_catalogueEntity> list;
                if (pagination == null)
                {
                    list = query.ToList();
                }
                else
                {
                    list = query.ToPageList(pagination.page, pagination.rows);
                }
                var users = Db.Queryable<UserEntity>().ToList();
                list?.ForEach(x =>
                {
                    x.CreateUserName = users.FirstOrDefault(y => y.F_UserId == x.CreateUserID)?.F_RealName;
                });
                return list;
            }
            catch (Exception ex)
            {
                if (ex is ExceptionEx)
                {
                    throw;
                }
                else
                {
                    throw ExceptionEx.ThrowBusinessException(ex);
                }
            }
        }
        /// <summary>
        /// 获取实体数据
        /// <param name="keyValue">主键</param>
        /// <summary>
        /// <returns></returns>
        public ec_template_catalogueEntity GetEntity(string keyValue, string ProjectId)
        {
            try
            {
                return ec_template_catalogueService.GetEntity(keyValue, ProjectId);
            }
            catch (Exception ex)
            {
                if (ex is ExceptionEx)
                {
                    throw;
                }
                else
                {
                    throw ExceptionEx.ThrowBusinessException(ex);
                }
            }
        }

        #endregion

        #region 提交数据

        /// <summary>
        /// 删除实体数据
        /// <param name="keyValue">主键</param>
        /// <summary>
        /// <returns></returns>
        public void DeleteEntity(string keyValue, string ProjectId)
        {
            try
            {
                ec_template_catalogueService.DeleteEntity(keyValue, ProjectId);
            }
            catch (Exception ex)
            {
                if (ex is ExceptionEx)
                {
                    throw;
                }
                else
                {
                    throw ExceptionEx.ThrowBusinessException(ex);
                }
            }
        }

        /// <summary>
        /// 保存实体数据（新增、修改）
        /// <param name="keyValue">主键</param>
        /// <summary>
        /// <returns></returns>
        public void SaveEntity(string keyValue, ec_template_catalogueEntity entity, string ProjectId)
        {
            try
            {
                ec_template_catalogueService.SaveEntity(keyValue, entity, ProjectId);
            }
            catch (Exception ex)
            {
                if (ex is ExceptionEx)
                {
                    throw;
                }
                else
                {
                    throw ExceptionEx.ThrowBusinessException(ex);
                }
            }
        }

        #endregion

    }
}
